//
//  XVVFoldConditionSearch.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/10.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVVFoldConditionSearch.h"

#import "XXTool.h"

@interface XVVFoldConditionSearch ()<XVVConditionalSearchDelegate>

@property (nonatomic,strong) UIView *backContent;



@end

@implementation XVVFoldConditionSearch
-(UIView*)backContent
{
    if (_backContent==nil)
    {
        _backContent = [[UIView alloc] init];
        _backContent.backgroundColor = [UIColor clearColor];
        _backContent.userInteractionEnabled = YES;
        [_backContent addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBackContent)]];
        [self addSubview:_backContent];
    }
    return _backContent;
}

-(XVVConditionalSearch*)searchView
{
    if (_searchView==nil)
    {
        _searchView = [[XVVConditionalSearch alloc] init];
        _searchView.delegate = self;
        [self addSubview:_searchView];
    }
    return _searchView;
}

-(void)initParameter
{
    _isShow = NO;
    self.hidden = YES;
}

-(void)initUI
{
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    [self backContent];
    [self searchView];
}

-(void)refreshLayout:(CGRect)frame
{
    _backContent.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    _searchView.frame = CGRectMake(0, 0, frame.size.width, [_searchView getViewHeight]);
}

-(void)clickBackContent
{
    [self dismiss];
}

#pragma mark XVVConditionalSearchDelegate
-(void)XVVConditionalSearchDidClickTableViewWithCellIndexPath:(NSIndexPath *)indexPath
{
    if ([_delegate respondsToSelector:@selector(XVVFoldConditionSearchDidClickTableViewCellWithIndexPath:)])
    {
        [_delegate XVVFoldConditionSearchDidClickTableViewCellWithIndexPath:indexPath];
    }
}

-(void)conditionalSearch:(XVVConditionalSearch *)seachView seachBarTextDidChange:(NSString *)changText
{
    if ([_delegate respondsToSelector:@selector(foldConditionSeach:seachBarTextDidChange:)])
    {
        [_delegate foldConditionSeach:self seachBarTextDidChange:changText];
    }
}

#pragma mark api
-(void)setConditionSearchData:(NSArray<XMLSearchConditionShowModel *> *)data
{
    [_searchView setUseData:data];
}

-(void)showStausChange
{
    if (!_isShow)
    {
        [self show];
    }
    else
    {
        [self dismiss];
    }
}

-(void)show
{
    self.backgroundColor = [UIColor clearColor];
    self.hidden = NO;
    _searchView.frame = CGRectMake(_searchView.frame.origin.x
                                   ,_searchView.frame.origin.y
                                   , _searchView.frame.size.width
                                   , 0);
    [[XXTool getViewController:self].view bringSubviewToFront:self];
    [UIView animateWithDuration:0.3f
                     animations:^{
                         self.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.6f];
                         _searchView.frame = CGRectMake(_searchView.frame.origin.x
                                                        ,_searchView.frame.origin.y
                                                        , _searchView.frame.size.width
                                                        , [_searchView getViewHeight]);
                     }];
    _isShow = !_isShow;
}

-(void)dismiss
{
    [UIView animateWithDuration:0.3f
                     animations:^{
                         self.backgroundColor = [UIColor clearColor];
                         _searchView.frame = CGRectMake(_searchView.frame.origin.x
                                                        ,_searchView.frame.origin.y
                                                        , _searchView.frame.size.width
                                                        , 0);
                     } completion:^(BOOL finished) {
                         if (finished)
                         {
                             self.hidden = YES;
                             [_searchView.searchbar resignFirstResponder];
                         }
                     }];
    _isShow = !_isShow;
}

@end
